package com.xyhy.core.base.service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.xyhy.core.base.model.dto.ObjectDTO;
import com.xyhy.core.base.entity.NamedObject;
import com.xyhy.core.base.repos.NamedCrudRepos;

public abstract class NamedCrudService<T extends NamedObject<ID>, ID, REPOS extends NamedCrudRepos<T, ID>> extends CrudService<T, ID, REPOS> {

  protected T existed(T data) {
    return repos.findFirstByNameIgnoringCase(data.getName());
  }

  public List<ObjectDTO<ID>> listOf(List<T> data) {
    return data.stream().sorted(Comparator.naturalOrder()).map(x -> new ObjectDTO<>(x.getId(), x.getName()))
            .collect(Collectors.toList());
  }
}
